import { ref, reactive, inject } from "vue";
import { useAuthorityStore } from '../../../store/useAuthorityStore';

interface Tree {
    id: string;
    label: string;
    value: string;
    children?: Tree[];
}

export const useAuthorityTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const authorityTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                authorityName: '',
                id: ''
            },
            authorityTreeData: ref<Tree[]>([]),
            authorityLevelData: ref([]),
        }
    );

    const authorityStore = useAuthorityStore();

    //查询
    const queryData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Authority.GetAllAuthority,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { authorityName: authorityTable.queryCondition.authorityName },
                }
            );

            if (response.status == "200" && response.data.code == "200") {
                authorityTable.tableData = response.data.data;
                authorityTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };
    ///所有菜单权限
    const fetchMenuAuthorityData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Authority.GetAllMenuAuthority
            );
            if (response.status == "200" && response.data.code == "200") {
                const transformMenuData: any = (AuthorityList: any[]) => {
                    return AuthorityList.map((authority) => {
                        return {
                            id: authority.id,
                            label: authority.authorityName,
                            value: authority.authorityName,
                        };
                    });
                };
                authorityTable.authorityTreeData = transformMenuData(response.data.data);
            }
        } catch (error) {
            console.error(error);
        }
    };
    ///所有权限
    const fetchAllAuthorityData = async (roleId: string) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Authority.GetAllAuthoritiesToLevel,
                { roleId: roleId }
            );

            if (response.status == "200" && response.data.code == "200") {
                authorityTable.authorityLevelData = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //设置权限与角色关系
    async function SetAuthorityToRole(roleId: string) {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Authority.SetAuthorityToRole,
                { roleId: roleId, childAuthorityList: authorityTable.authorityLevelData }
            );

            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Authority.Create,
                authorityStore.authorityModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Authority.Update,
                authorityStore.authorityModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (authorityId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Authority.DeleteByAuthorityId,
                { 'authorityId': authorityId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    return { authorityTable, authorityStore, queryData, fetchMenuAuthorityData, fetchAllAuthorityData, SetAuthorityToRole, addData, updateData, deleteData };
};